/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import general.util.EntitiesValidation;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import jpa.controller.CargoJpaController;
import jpa.entity.Cargo;

/**
 *
 * @author isaias
 */
@ManagedBean
@ViewScoped
public class CargoBean {

    private Cargo cargo = new Cargo();
    private Cargo cargoSelected = new Cargo();
    private String nameCargo = null;
    
    /**
     * Creates a new instance of CargoBean
     */
    public CargoBean() {
        
    }

    public void save(){
        List<String> erros = EntitiesValidation.validateCargo(cargo);

        if(erros.isEmpty()){
           try{
            new CargoJpaController().create(cargo);
             FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
                    "Cargo criado com sucesso!", null));
            cargo = new Cargo();
            }catch(Exception ex){
                ex.printStackTrace();
                   FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    "Erro ao criar cargo!", null));
            }
        }else{
           for( String e : erros){
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    e, null));
           }
        }
    }
    
    public void edit(){
         List<String> erros = EntitiesValidation.validateCargo(cargoSelected);

         if(erros.isEmpty()){
              try {
                  if(cargoSelected.getId() != null){            
                     new CargoJpaController().edit(cargoSelected);
                        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
                        "Cargo editado com sucesso!", null));
                       cargoSelected = new Cargo();
                  }else{
                       FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    "Selecione um cargo para poder editar!", null));
                  }
             } catch (Exception e) {
                       FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    "Erro ao tentar editar o cargo selecionado!", null));
                 e.printStackTrace();
             }

         }else{
             for( String e : erros){
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    e, null));
           }
         }

    }
    
    public void cargoDelete(){
        if(cargoSelected.getId() != null){
            try {
                new CargoJpaController().destroy(cargoSelected.getId());
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
                            "Cargo apagado com sucesso!", null));
            } catch (Exception e) {
               e.printStackTrace();
                 FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL,
                            "Erro ao tentar apagar cargo!", null));
            }
        }
    }
    
    public List<Cargo> cargosList(){
        return new CargoJpaController().findCargoEntities();
    }
    
    public Cargo getCargo() {
        return cargo;
    }

    public void setCargo(Cargo cargo) {
        this.cargo = cargo;
    }

    public Cargo getCargoSelected() {
        return cargoSelected;
    }

    public void setCargoSelected(Cargo cargoSelected) {
        this.cargoSelected = cargoSelected;
    }

    public String getNameCargo() {
        return nameCargo;
    }

    public void setNameCargo(String nameCargo) {
        this.nameCargo = nameCargo;
    }
    
    
    
}
